//
//  TTBeautyManagerDetailViewController.m
//  TaTaBang
//
//  Created by 李禹 on 15/8/30.
//  Copyright (c) 2015年 hailong. All rights reserved.
//

#import "TTBeautyManagerDetailViewController.h"
#import "NJSettingSwitchItem.h"
#import "NJSettingArrowItem.h"
#import "NJSettingGroup.h"
#import "NJProductCell.h"
#import "NJSettingTextFieldItem.h"
#import "NJSettingButtonItem.h"
#import "TTSettingLabelItem.h"

@interface TTBeautyManagerDetailViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation TTBeautyManagerDetailViewController

#pragma mark - 懒加载
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        // 第一组数据
        NJSettingItem *item00 = [[TTSettingLabelItem alloc] initWithTitle:@"手机号" detailText:@"请输入手机号"];
        NJSettingItem *item01 = [[TTSettingLabelItem alloc] initWithTitle:@"犬主名" detailText:@"安在宣"];
        NJSettingItem *item02 = [[TTSettingLabelItem alloc] initWithTitle:@"会员卡余额" detailText:@"￥1500.0" textColor:[UIColor redColor]];
        
        NJSettingGroup *group1 = [[NJSettingGroup alloc] init];
        group1.items = @[item00, item01, item02] ;
        
        // 第2组数据
        NJSettingItem *item10 = [[TTSettingLabelItem alloc] initWithTitle:@"爱宠名" detailText:@"旺财"];
        NJSettingItem *item11 = [[NJSettingButtonItem alloc] initWithTitle:@"爱宠照片" iconName:@"pet"];
        __weak typeof(self) this = self;
        item11.option = ^() {
            UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:nil delegate:this cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从手机相册选择", nil];
            menu.actionSheetStyle = UIBarStyleBlackTranslucent;
            [menu showInView:this.view];
        };
        NJSettingGroup *group2 = [[NJSettingGroup alloc] init];
        group2.items = @[item10, item11];
        
        // 第3组数据
        NJSettingItem *item20 = [[TTSettingLabelItem alloc] initWithTitle:@"类别" detailText:@"美容"];
        NJSettingItem *item21 = [[TTSettingLabelItem alloc] initWithTitle:@"美容方式" detailText:@"手剪"];
        NJSettingItem *item22 = [[TTSettingLabelItem alloc] initWithTitle:@"美容日期" detailText:@"2015/09/01"];
        NJSettingItem *item23 = [[TTSettingLabelItem alloc] initWithTitle:@"消费金额" detailText:@"￥150" textColor:[UIColor redColor]];
        NJSettingItem *item24 = [[TTSettingLabelItem alloc] initWithTitle:@"消费次数" detailText:@"1次"];
        NJSettingItem *item25 = [[TTSettingLabelItem alloc] initWithTitle:@"服务项目" detailText:@"手剪美容"];
        NJSettingItem *item26 = [[TTSettingLabelItem alloc] initWithTitle:@"服务人员" detailText:@"小丽"];
        NJSettingItem *item27 = [[TTSettingLabelItem alloc] initWithTitle:@"备注" detailText:@"送一次洗澡"];

        NJSettingGroup *group3 = [[NJSettingGroup alloc] init];
        group3.items = @[item20, item21, item22, item23, item24, item25, item26, item27];
        
        _dataArray = [NSMutableArray array];
        [_dataArray addObject:group1];
        [_dataArray addObject:group2];
        [_dataArray addObject:group3];
    }
    return _dataArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"查看美容服务详情";
    self.tableView.rowHeight = 44;
//    self.view.backgroundColor = [UIColor whiteColor];

}

- (id)init {
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (id)initWithStyle:(UITableViewStyle)style {
    return [super initWithStyle:UITableViewStyleGrouped];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 先取出对应组的小数组
    NJSettingGroup *g = self.dataArray[section];
    // 返回小数组的长度
    return g.items.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 1.创建cell
    NJProductCell *cell = [NJProductCell cellWithTableView:tableView];
    
    // 先取出对应组的组模型
    NJSettingGroup *g = self.dataArray[indexPath.section];
    //  从组模型中取出对应行的模型
    NJSettingItem *item = g.items[indexPath.row];
    cell.item = item;
    
    // 3.返回cell
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 立即取消选中
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 先取出对应组的组模型
    NJSettingGroup *g = self.dataArray[indexPath.section];
    //  从组模型中取出对应行的模型
    NJSettingItem *item = g.items[indexPath.row];
    // 判断block中是否保存了代码
    if (item.option != nil) {
        // 如果保存,就执行block中保存的代码
        item.option();
    }else if ([item isKindOfClass:[NJSettingArrowItem class]]) {
        
    }
}

- (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section == 1 && indexPath.row == 1) {
        return 80;
    }else {
        return 44;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    // 先取出对应组的组模型
    NJSettingGroup *g = self.dataArray[section];
    return g.headerTitle;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    // 先取出对应组的组模型
    NJSettingGroup *g = self.dataArray[section];
    return g.footerTitle;
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(nonnull UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePicker.delegate = self;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }else {
            UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"相机不能用" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            [alert show];
        }
    }else if(buttonIndex == 1){
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
        
        UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
        ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        ipc.delegate = self;
        [self presentViewController:ipc animated:YES completion:nil];
    }
    
}

#pragma mark UIImagePickerController Method
//完成拍照
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
    
//    UIImage *image = info[UIImagePickerControllerOriginalImage];
}

//用户取消拍照
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}


@end
